"ui";
activity.window.addFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.window.setStatusBarColor(android.graphics.Color.TRANSPARENT);
activity.window.getDecorView().setSystemUiVisibility(android.view.View.SYSTEM_UI_FLAG_LAYOUT_STABLE | android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
var random_up, auto_up_down ;

ui.layout(
    <vertical>
        <linear id="appbar" orientation="vertical" h="90">
            <View h="40" w="*"/>
            <frame h="50">
                <img id="back" src="@drawable/ic_arrow_back_black_48dp" padding="12" tint="#ffffff" w="50" h="50" foreground="?selectableItemBackground"/>
                <text id="app_name" textSize="18sp" textStyle="bold" textColor="#ffffff" w="*" text="京东商品库存查询" h="50" gravity="center"/>
            </frame>
        </linear>
        <linear orientation="horizontal" w="*" h="40" margin="5 5 0 0" gravity="center" layout_gravity="center">
            <card w="0" h="36" cardCornerRadius="18dp" cardElevation="2" layout_weight="1" marginRight="15">
                <input id="skuids_url" text="100012043978" hint="京东分享地址" marginLeft="10" marginRight="10"  gravity="center_vertical" textSize="14sp" textColor="black" bg="#ffffff" w="*" h="*"/>
            </card>
            <card h="36" w="80sp" cardCornerRadius="18dp" cardElevation="0" marginRight="15">
                <button id="btn_search" text="查询" textColor="#FFFFFF" gravity="center" textSize="14sp" bg="#FF1CBBB4" />
            </card>
        </linear>
        <vertical h="*">
            <list id="video_list" layout_weight="1">
                <vertical h="41" w="*" foreground="?selectableItemBackground">
                    
                    <linear orientation="horizontal" margin="5" >
                        <linear orientation="horizontal" w="*" h="30">
                            <text id="area_name" textSize="14sp" textStyle="bold" w="*" layout_weight="1" textColor="black" marginLeft="15" text="{{name}}" h="*" gravity="center_vertical"/>

                            <text id="area_stock" textSize="14sp" textColor="{{textColor}}" marginRight="15" text="{{stock}}" h="*" gravity="center_vertical"/>
                        </linear>
                    </linear>

                    <View w="*" h="1" bg="#E0E0E0"/>
                </vertical>
            </list>
            <linear orientation="horizontal" h="50" background="#f4f5f6" >
                <text id="result_msg" text="一共0条，已经查询 0 个结果" textColor="#000000" textStyle="bold" gravity="center" textSize="16sp" w="*" h="*"/>
            </linear>
        </vertical>
    </vertical>
);

ui.appbar.backgroundDrawable = GradientDrawable("LEFT_RIGHT", ["#FF0081FF", "#FF1CBBB4"]);
// ui.start.backgroundDrawable = GradientDrawable("LEFT_RIGHT", ["#FF0081FF", "#FF1CBBB4"]);
var storage = storages.create("jd_stock");

var num = 0;
let json = null;
var num_ = 1;
//是否开启滑动模式，已经开启不在开启，保证之开启一次，避免冲突
var is_start_slide = false;
//程序已经执行次数
var run_num = 1;
//单个app的开始时间和结束时间
var start_run_time;
var end_run_time;
//已经选中的app
let list = JSON.parse("[]");
//获取网络数据
threads.start(getDatas);

var lock = threads.lock();

/**
 * 转换成系统支持的颜色
 * @param {颜色} color 
 */
 function Color(color) {
    return android.graphics.Color.parseColor(color);
}

/**
 * 配置渐变色
 * @param {方向} orientation 
 * @param {颜色数组} color 
 */
function GradientDrawable(orientation, color) {
    var colors = [];
    color.forEach(color => colors.push(Color(color)));
    return new android.graphics.drawable.GradientDrawable(android.graphics.drawable.GradientDrawable.Orientation[orientation], colors);
}

function getDatas(){
    
    var Url = "https://gitee.com/wupk/auto.test/raw/master/test/wine/jd_area.json";
    var res_script = http.get(Url, {
        headers: {
            'Accept-Language': 'en-us,en;q=0.5',
            'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11'
        }
    });
    var temp = res_script.body.string();
    if (res_script.statusCode == 200){
        json = JSON.parse(temp);
        for(var i=0;i<json.length;i++){
            json[i].stock = "无";
            json[i].textColor = "red";
            // black
        }
        //获取到数据以后重新计算一下app是否安装，和已经缓存的数据，缓存的数据放到最上方
        ui.post(function(action, delay){
            setDatas(json);
        });
    }
}

function setDatas(json){
    ui.result_msg.setText("一共 "+json.length+" 条，已经查询 0 个结果");
    ui.video_list.setDataSource(json);
}


ui.btn_search.click(()=> {
    var skuIds = ui.skuids_url.getText().toString();
    var path = "http://c0.3.cn/stocks?type=batchstocks&pdpin=&_=1501582930149&skuIds="+skuIds+"&area=1_0_72";
    // for(var i=0;i<1;i++){
    //     var url = path+json[i].code;
    //     threads.start();
    // }

    getHttp(0,path)
});

function getHttp(i,url){
    lock.lock();
    console.log("----------------------------"+url);
    var res_script = http.get(url, {
        headers: {
            'Accept-Language': 'en-us,en;q=0.5',
            'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11'
        }
    });
    var temp = res_script.body.string();
    var stockJson = JSON.parse(temp);
    toast(temp);
    lock.unlock();
}